package cn.cxj.pet.service.impl;

import cn.cxj.basic.service.impl.BaseServiceImpl;
import cn.cxj.basic.util.DistanceUtil;
import cn.cxj.basic.util.PageList;
import cn.cxj.basic.util.Point;
import cn.cxj.org.domain.Employee;
import cn.cxj.org.domain.Shop;
import cn.cxj.org.mapper.EmployeeMapper;
import cn.cxj.org.mapper.ShopMapper;
import cn.cxj.pet.domain.Pet;
import cn.cxj.pet.domain.SearchMasterMsg;
import cn.cxj.pet.mapper.SearchMasterMsgMapper;
import cn.cxj.pet.query.SearchMasterMsgQuery;
import cn.cxj.pet.service.IPetService;
import cn.cxj.pet.service.ISearchMasterMsgService;
import cn.cxj.user.domain.Logininfo;
import cn.cxj.user.domain.User;
import cn.cxj.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private IPetService petService;

    @Override
    public void publish(SearchMasterMsg searchMasterMsg, Logininfo logininfo) {
        // 设置状态为待处理
        searchMasterMsg.setState(0);
        // 根据地点就近分配店铺来处理宠物
        List<Shop> shops = shopMapper.loadAll();
        // 设置searchMasterMsg的user_id
        User user = userMapper.loadByLogininfoId(logininfo.getId());
        searchMasterMsg.setUser_id(user.getId());
        // 先获取到宠物所在位置的经纬度
        Point point = DistanceUtil.getPoint(searchMasterMsg.getAddress());
        Shop nearestShop = DistanceUtil.getNearestShop(point, shops);
        // 设置searchMasterMsg的shop_id
        searchMasterMsg.setShop_id(nearestShop.getId());
        // 保存searchMasterMsg到数据库
        super.add(searchMasterMsg);
    }

    /**
     * 待处理的寻主消息
     *
     * @param query
     * @param logininfo
     * @return
     */
    @Override
    public PageList<SearchMasterMsg> pending(SearchMasterMsgQuery query, Logininfo logininfo) {
        // 设置状态为待处理
        query.setState(0);
        // 获取当前登录用户所属店铺
        Employee employee = employeeMapper.loadByLogininfoId(logininfo.getId());
        // 将当前员工所属的店铺的id设置给query
        if (employee.getShop_id() != null) {
            query.setShopId(employee.getShop_id());
        }
        return super.queryPage(query);
    }

    /**
     * 已处理的寻主消息
     * @param query
     * @param logininfo
     * @return
     */
    @Override
    public PageList<SearchMasterMsg> processed(SearchMasterMsgQuery query, Logininfo logininfo) {
        // 设置状态为已处理
        query.setState(1);
        Employee employee = employeeMapper.loadByLogininfoId(logininfo.getId());
        if (employee.getShop_id() != null) {
            query.setShopId(employee.getShop_id());
        }
        return super.queryPage(query);
    }

    @Override
    @Transactional
    public void handle(Pet pet, Logininfo logininfo) {
        // 修改消息状态
        searchMasterMsgMapper.updateState2processedById(pet.getSearch_master_msg_id());
        // 新增宠物信息和详情信息,调用IpetService实现类完成业务
        petService.add(pet);
        //@TODO 生成订单
        //@TODO 生成支付单
    }
}
